package com.deeper.customview.ui.c9.demo.c09d02;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.deeper.customview.BaseFragment;
import com.deeper.customview.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Chapter09Demo02Fragment extends BaseFragment {
    private FlingRemovedListView listView;
    private SimpleAdapter simpleAdapter;
    private final List<Map<String, String>> list = new ArrayList<>();


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_flingremoved_listview, container, false);
        init(root);
        return root;
    }


    private void init(View view) {
        for (int i = 0; i < 50; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("name", "李赞红" + i);
            map.put("company", "韬睿科技");
            list.add(map);
        }
        listView = view.findViewById(R.id.flingRemovedListView);
        simpleAdapter = new SimpleAdapter(getContext(), list, R.layout.item, new String[]{"name", "company"}, new int[]{R.id.name, R.id.company});
        listView.setAdapter(simpleAdapter);

        listView.setOnRemovedItemListener(new FlingRemovedListView.OnRemovedItemListener() {
            @Override
            public void itemRemoved(int position, ListAdapter adapter) {
                list.remove(position);
                ((SimpleAdapter) adapter).notifyDataSetChanged();
            }
        });
    }
}
